package com.dingwei.superiordiary.mvp.presenter;

import android.app.Dialog;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.easyViewHolder.CheckAreaHolder;
import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.Area_Bean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.google.gson.Gson;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.RegexUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.net.URL;
import java.util.List;

/**
 * Created by lihao on 2017/9/12.
 */

public class AddAddressPresenter extends basePresenter {

    private Contract.AddAddressView addAddressView;
    private Dialog dlg;


    private String provinceName;
    private String cityName;
    private String countyName;
    private String provinceId;
    private String cityId;
    private String countyid;
    public AddAddressPresenter(Contract.AddAddressView view) {
        super(view);
        this.addAddressView = view;
    }


    /**
     * 获取省份列表数据
     */
    public void getProvince() {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.AREA_INDEX);

        rp.addBodyParameter("parent_id","1");
        x.http().post(rp, new myCallback(BaseActivity.context, Area_Bean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                Area_Bean bean = new Gson().fromJson(result,Area_Bean.class);
                showProvinceDialog(bean.data);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    RecyclerArrayAdapter mAdapter = new RecyclerArrayAdapter(BaseActivity.context) {
        @Override
        public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
            return new CheckAreaHolder(parent);
        }
    };

    /**
     * 省份列表Dialog
     * @param data 省份列表数据
     */
    public void showProvinceDialog(List<Area_Bean.DataBean> data) {

        dlg = new Dialog(BaseActivity.context, R.style.Theme_Light_Dialog);
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_check_city, null);
        view.measure(0,0);


        ImageView imgClose = (ImageView) view.findViewById(R.id.img_close);
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                provinceName = "";
                provinceId = "";
                cityName = "";
                cityId = "";
                countyName = "";
                countyid = "";
                dlg.dismiss();
            }
        });

        EasyRecyclerView mRecycleView = (EasyRecyclerView) view.findViewById(R.id.mRecycleView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.getRecyclerView().setNestedScrollingEnabled(false);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.addItemDecoration(new DividerDecoration(BaseActivity.context.getResources().getColor(R.color.parting_line), 1));

        mAdapter.clear();
        mAdapter.addAll(data);

        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Area_Bean.DataBean bean = (Area_Bean.DataBean) mAdapter.getItem(position);
                getCity(bean.id);
                provinceId = bean.id;
                provinceName = bean.name;
                dlg.dismiss();

            }
        });

        dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dlg.setContentView(view);
        Window dialogWindow = dlg.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        dlg.show();
    }


    /**
     * 获取城市列表数据
     * @param id 省份ID
     */
    public void getCity(String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.AREA_INDEX);
        rp.addBodyParameter("parent_id",id);
        x.http().post(rp, new myCallback(BaseActivity.context, Area_Bean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                Area_Bean bean = new Gson().fromJson(result,Area_Bean.class);
                showCityDialog(bean.data);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    /**
     * 城市列表Dialog
     * @param data 城市列表数据
     */
    public void showCityDialog(List<Area_Bean.DataBean> data) {

        dlg = new Dialog(BaseActivity.context,R.style.Theme_Light_Dialog);
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_check_city, null);


        ImageView imgClose = (ImageView) view.findViewById(R.id.img_close);
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                provinceName = "";
                provinceId = "";
                cityName = "";
                cityId = "";
                countyName = "";
                countyid = "";
                dlg.dismiss();
            }
        });

        EasyRecyclerView mRecycleView = (EasyRecyclerView) view.findViewById(R.id.mRecycleView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.getRecyclerView().setNestedScrollingEnabled(false);
        mRecycleView.setAdapter(mAdapter);
        mRecycleView.addItemDecoration(new DividerDecoration(BaseActivity.context.getResources().getColor(R.color.parting_line), 1));
        mAdapter.clear();
        mAdapter.addAll(data);
        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Area_Bean.DataBean bean = (Area_Bean.DataBean) mAdapter.getItem(position);
                getCounty(bean.id);
                cityId = bean.id;
                cityName = bean.name;
                dlg.dismiss();
            }
        });
        dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dlg.setContentView(view);
        Window dialogWindow = dlg.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        dlg.show();
    }


    /**
     * 获取地方列表数据
     * @param id 城市ID
     */
    public void getCounty(String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.AREA_INDEX);
        rp.addBodyParameter("parent_id",id);
        x.http().post(rp, new myCallback(BaseActivity.context, Area_Bean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                Area_Bean bean = new Gson().fromJson(result,Area_Bean.class);
                showCountyDialog(bean.data);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    /**
     * 地区列表Dialog
     * @param data 地区列表数据
     */
    public void showCountyDialog(List<Area_Bean.DataBean> data) {

        dlg = new Dialog(BaseActivity.context,R.style.Theme_Light_Dialog);
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_check_city, null);


        ImageView imgClose = (ImageView) view.findViewById(R.id.img_close);
        imgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                provinceName = "";
                provinceId = "";
                cityName = "";
                cityId = "";
                countyName = "";
                countyid = "";
                dlg.dismiss();
            }
        });

        EasyRecyclerView mRecycleView = (EasyRecyclerView) view.findViewById(R.id.mRecycleView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.getRecyclerView().setNestedScrollingEnabled(false);
        mRecycleView.setAdapter(mAdapter );


        mRecycleView.addItemDecoration(new DividerDecoration(BaseActivity.context.getResources().getColor(R.color.parting_line), 1));
        mAdapter.clear();
        mAdapter.addAll(data);
        mAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                dlg.dismiss();
                Area_Bean.DataBean bean = (Area_Bean.DataBean) mAdapter.getItem(position);
                countyid = bean.id;
                countyName = bean.name;
                addAddressView.checkCity(provinceId,provinceName,cityId,cityName,countyid,countyName);
            }
        });
        dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dlg.setContentView(view);
        Window dialogWindow = dlg.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        android.view.WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        dlg.show();
    }

    /**
     * 保存收货地址
     * @param name 收货人姓名
     * @param mobile 收货人电话
     * @param provinceId 省份ID
     * @param cityId 城市ID
     * @param countyId 地区ID
     * @param address 详细地址
     * @param id 收货地址ID  空为添加 有值为编辑
     */
    public void keep(String name, String mobile, String provinceId, String cityId, String countyId, String address, String id) {

        if (TextUtils.isEmpty(name)){
            addAddressView.showToast("请输入收货人姓名");
            return;
        }
        if (name.length()<2){
            addAddressView.showToast("收货人姓名必须大于等于两个字");
            return;
        }
        if (TextUtils.isEmpty(mobile)){
            addAddressView.showToast("请输入收货人电话");
            return;
        }
        if (!RegexUtils.isMobileExact(mobile)){
            addAddressView.showToast("请输入正确的收货人电话");
            return;
        }
        if (TextUtils.isEmpty(provinceId)){
            addAddressView.showToast("请选择收货地区");
            return;
        }
        if (TextUtils.isEmpty(address)){
            addAddressView.showToast("请输入收货详细地址");
            return;
        }


        RequestParams rp = new RequestParams(URLS.HOST+ URLS.SAVE_ADDRESS);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());
        rp.addBodyParameter("name",name);
        rp.addBodyParameter("mobile",mobile);
        rp.addBodyParameter("province",provinceId);
        rp.addBodyParameter("city",cityId);
        rp.addBodyParameter("county",countyId);
        rp.addBodyParameter("address",address);
        if (!TextUtils.isEmpty(id)){
            rp.addBodyParameter("id",id);
        }

        x.http().post(rp, new myCallback(BaseActivity.context,MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                addAddressView.saveAddressSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });

    }
}
